package rs.belex.belexdroid.services.impl.ksoap2;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

import org.ksoap2.serialization.SoapObject;

import rs.belex.belexdroid.beans.QuoteBean;
import rs.belex.belexdroid.beans.StockQuoteBean;
import rs.belex.belexdroid.beans.interfaces.Quote;
import rs.belex.belexdroid.beans.interfaces.StockQuote;
import rs.belex.belexdroid.services.interfaces.IStockQuoteService;
import android.util.Log;

public class StockQuoteServiceKsoap2Impl extends ServiceClientKsoap2Impl
		implements IStockQuoteService {
	private static String TAG = StockQuoteServiceKsoap2Impl.class
			.getSimpleName();

	public StockQuoteServiceKsoap2Impl() {
	};

	public List<Quote> getAllQuotes(String symbol) {
		List<Quote> quotes = new LinkedList<Quote>();
		Log.d(TAG, "Get all Quotes called");
		final String SOAP_ACTION = "";
		final String METHOD_NAME = "getAllQuotesBySymbol";

		// Creating input parameters
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		request.addProperty("symbol", symbol);

		List<SoapObject> response = super.requestComplexTypeList(request,
				SOAP_ACTION);

		// Iterate through response and make a List of shares
		for (SoapObject s : response) {
			Quote quote = new QuoteBean();
			Date date = dateParser(s.getProperty("date").toString());

			quote.setDate(date);
			quote.setChangePercent(Double.parseDouble(s.getProperty(
					"changePercent").toString()));
			quote.setClosingPrice(Double.parseDouble(s.getProperty(
					"closingPrice").toString()));
			try {
				quote.setOpeningPrice(Double.parseDouble(s.getProperty(
						"openingPrice").toString()));
				quote.setDayHighPrice(Double.parseDouble(s.getProperty(
						"dayHighPrice").toString()));
				quote.setDayLowPrice(Double.parseDouble(s.getProperty(
						"dayLowPrice").toString()));
			} catch (NullPointerException e) {
				Log
						.i(TAG,
								"Values with null in Quote, skippin initialization of that fields");
			}
			try {
				quote.setBuyingVolume(Integer.parseInt(s.getProperty(
						"buyingVolume").toString()));
				quote.setSellingVolume(Integer.parseInt(s.getProperty(
						"sellingVolume").toString()));
			} catch (NullPointerException e) {
				Log
						.i(TAG,
								"Values with null in Quote, skippin initialization of that fields");
			}
			quote.setVolume(Integer
					.parseInt(s.getProperty("volume").toString()));
			quote.setVolumePrice(Double.parseDouble(s
					.getProperty("volumePrice").toString()));

			quotes.add(quote);
		}

		Log.d(TAG, "Get All Quotes successful");

		return quotes;
	}

	public StockQuote getStockQuote(String symbol) {
		StockQuote stockQuote = null;
		Log.d(TAG, "getStockQuote called");
		final String SOAP_ACTION = "";
		final String METHOD_NAME = "getRecentStockQuote";

		// Creating input parameters
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		request.addProperty("symbol", symbol);

		SoapObject response = (SoapObject) super.requestComplexType(request,
				SOAP_ACTION);

		if (response != null) {
			// Parsing response to StockQuote
			try {
				stockQuote = new StockQuoteBean();
				Date date = dateParser(response.getProperty("date").toString());

				stockQuote.setDate(date);
				stockQuote.setSymbol(response.getProperty("symbol").toString());
				stockQuote.setCompanyName(response.getProperty("companyName")
						.toString());
				stockQuote.setLastTradedPrice(Double.parseDouble(response
						.getProperty("lastTradedPrice").toString()));
				stockQuote.setChangePercent(Double.parseDouble(response
						.getProperty("changePercent").toString()));
				stockQuote.setClosingPrice(Double.parseDouble(response.getProperty(
						"closingPrice").toString()));
				/** Usually they all go together */
				try {
					stockQuote.setOpeningPrice(Double.parseDouble(response
							.getProperty("openingPrice").toString()));
					stockQuote.setDayHighPrice(Double.parseDouble(response
							.getProperty("dayHighPrice").toString()));
					stockQuote.setDayLowPrice(Double.parseDouble(response
							.getProperty("dayLowPrice").toString()));
				} catch (NullPointerException e) {
					Log
							.i(TAG,
									"Values with null in Quote, skippin initialization of that fields");
				}
				/** Usually they all go together */
				try {
					stockQuote.setBuyingVolume(Integer.parseInt(response
							.getProperty("buyingVolume").toString()));
					stockQuote.setSellingVolume(Integer.parseInt(response
							.getProperty("sellingVolume").toString()));
				} catch (NullPointerException e) {
					Log
							.i(TAG,
									"Values with null in Quote, skippin initialization of that fields");
				}
				stockQuote.setVolume(Integer.parseInt(response.getProperty(
						"volume").toString()));
				stockQuote.setVolumePrice(Double.parseDouble(response
						.getProperty("volumePrice").toString()));
				stockQuote.setType(response.getProperty("type").toString());
				stockQuote.setMethod(response.getProperty("method").toString());

				Log.d(TAG, "StockQuote created successfully");
			} catch (Exception e) {
				Log
						.e(TAG,
								"Error while parsing response to StockQuote type of Object");
				e.printStackTrace();
			}
		}
		return stockQuote;
	}

	public List<StockQuote> getStockQuotes(List<String> symbols) {
		List<StockQuote> stockQuotes = new LinkedList<StockQuote>();
		Log.d(TAG, "Get Stock Quotes called");
		final String SOAP_ACTION = "";
		final String METHOD_NAME = "getRecentStockQuotes";

		// ksoap2 requires Vector to be used as a collection so we must convert
		// it into one
		Vector<String> symbolsVector = new Vector<String>(symbols);

		// Creating input parameters
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		request.addProperty("symbols", symbolsVector);

		List<SoapObject> response = super.requestComplexTypeList(request,
				SOAP_ACTION);

		// Iterate through response and make a List of stockQuotes
		for (SoapObject s : response) {
			StockQuote stockQuote = new StockQuoteBean();
			Date date = dateParser(s.getProperty("date").toString());

			stockQuote.setDate(date);
			stockQuote.setSymbol(s.getProperty("symbol").toString());
			stockQuote.setCompanyName(s.getProperty("companyName").toString());
			stockQuote.setLastTradedPrice(Double.parseDouble(s.getProperty(
					"lastTradedPrice").toString()));
			stockQuote.setChangePercent(Double.parseDouble(s.getProperty(
					"changePercent").toString()));
			stockQuote.setClosingPrice(Double.parseDouble(s.getProperty(
					"closingPrice").toString()));
			try {
				stockQuote.setOpeningPrice(Double.parseDouble(s.getProperty(
						"openingPrice").toString()));
				stockQuote.setDayHighPrice(Double.parseDouble(s.getProperty(
						"dayHighPrice").toString()));
				stockQuote.setDayLowPrice(Double.parseDouble(s.getProperty(
						"dayLowPrice").toString()));
			} catch (NullPointerException e) {
				Log
						.i(TAG,
								"Values with null in Quote, skippin initialization of that fields");
			}
			try {
				stockQuote.setBuyingVolume(Integer.parseInt(s.getProperty(
						"buyingVolume").toString()));
				stockQuote.setSellingVolume(Integer.parseInt(s.getProperty(
						"sellingVolume").toString()));
			} catch (NullPointerException e) {
				Log
						.i(TAG,
								"Values with null in Quote, skippin initialization of that fields");
			}
			stockQuote.setVolume(Integer.parseInt(s.getProperty("volume")
					.toString()));
			stockQuote.setVolumePrice(Double.parseDouble(s.getProperty(
					"volumePrice").toString()));
			stockQuote.setType(s.getProperty("type").toString());
			stockQuote.setMethod(s.getProperty("method").toString());

			stockQuotes.add(stockQuote);
		}

		Log.d(TAG, "Get StockQuotes successful");

		return stockQuotes;
	}
}
